@echo off

REM Check if clang-format is available
where clang-format > nul
if %errorlevel% neq 0 (
    echo Error: clang-format not found. Please install LLVM tools first.
    echo Download from: https://llvm.org/builds/
    pause
    exit /b 1
)

REM Display clang-format version information
clang-format --version

REM Set target directory
set CPP_DIR=.\src\main\cpp
set CLANG_FORMAT_FILE=%CPP_DIR%\.clang-format

REM Check if .clang-format configuration file exists
if not exist "%CLANG_FORMAT_FILE%" (
    echo Warning: Cannot find %CLANG_FORMAT_FILE%. Using default configuration.
)

REM Format specific directories and file
set CPP_BASE=.\src\main\cpp
set INCLUDE_DIR=%CPP_BASE%\include
set SRC_DIR=%CPP_BASE%\src
set NAPI_FILE=%CPP_BASE%\napi_init.cpp

REM Format .cpp and .h files in include directory
echo Formatting files in %INCLUDE_DIR%
for /r "%INCLUDE_DIR%" %%f in (*.cpp *.h) do (
    REM Skip thread_state_pls.h to preserve include order
    if not "%%~nf%%~xf" == "thread_state_pls.h" (
        echo Formatting: %%f
        clang-format -i "%%f"
    ) else (
        echo Skipping: %%f (preserving include order)
    )
)

REM Format .cpp and .h files in src directory
echo Formatting files in %SRC_DIR%
for /r "%SRC_DIR%" %%f in (*.cpp *.h) do (
    echo Formatting: %%f
    clang-format -i "%%f"
)

REM Format the specific napi_init.cpp file
if exist "%NAPI_FILE%" (
    echo Formatting specific file: %NAPI_FILE%
    clang-format -i "%NAPI_FILE%"
) else (
    echo Warning: %NAPI_FILE% not found!
)

echo Formatting completed!
pause